package com.sailorj.service.impl;

import com.sailorj.entity.TbLab;
import com.sailorj.mapper.TbLabMapper;
import com.sailorj.service.TbLabService;
import com.sailorj.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbLabServiceImpl implements TbLabService {

    @Autowired
    public TbLabMapper labMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void addLab(TbLab tbLab) {
        labMapper.insert(tbLab);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void updateLabById(TbLab tbLab) {
        labMapper.updateByPrimaryKey(tbLab);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbLab getLabById(int id) {
        return labMapper.selectByPrimaryKey(id);
    }

//    @Override
//    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
//    public List<TbLab> getLabList() {
//        return labMapper.selectLabList();
//    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getLabList(Integer page, Integer size) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbLab> data = labMapper.selectLabList(page,size);
        Long total=labMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbLab findByLabName(String name) {
        return labMapper.findLabByName(name);
    }
}
